package com.xja.service;/**
 * @className: MyUserDetailsService
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/12
 **/



import com.xja.pojo.Admin;
import com.xja.pojo.Role;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : MyUserDetailsService  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/12  15:19
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

    @Reference(url = "dubbo://localhost:20882")
    private AdminService adminService;
    @Reference(url = "dubbo://localhost:20882")
    private RoleService roleService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Admin admin = adminService.login(s);
        if (admin==null){
            throw new UsernameNotFoundException("未找到用户");
        }
        List<Role> rolesList = roleService.getRolesListByAid(admin.getAdminId());
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role roles : rolesList) {
            authorities.add(new SimpleGrantedAuthority(roles.getDescription()));
        }
        return new User(admin.getAdminName(),admin.getAdminPass(),authorities);
    }
}
